In this tutorial, we are going to use Neural Network to do image classification. The following figure shows a simple example of Neural Network. If you are interested in this field, please see this review or recently this review.
The MNIST database is a large database of handwritten digits. It contains 60,000 training images and 10,000 testing images. The Keras provides a convenience method for loading the MNIST dataset.
# In this demo we design a Neural Network with 1 hidden layers:# Input layer (784 dimensions)# layer 1 (1000 hidden neurons)# layer 2 (10 outputs)import numpyfrom keras.datasets import mnistfrom keras.models import Sequentialfrom keras.layers import Densefrom keras.utils import np_utils# load data(X_train, y_train), (X_test, y_test) = mnist.load_data()# flatten 28*28 images to a 784 vector for each imagenum_pixels = X_train.shape[1] * X_train.shape[2]X_train = X_train.reshape(X_train.shape[0], num_pixels).astype('float32')X_test = X_test.reshape(X_test.shape[0], num_pixels).astype('float32')# normalize inputs from 0-255 to 0-1X_train = X_train / 255X_test = X_test / 255# one hot encode outputsy_train = np_utils.to_categorical(y_train)y_test = np_utils.to_categorical(y_test)num_classes = y_test.shape[1]# design modelmodel = Sequential()model.add(Dense(1000, input_dim=num_pixels, activation='relu'))model.add(Dense(num_classes, activation='softmax'))# print out summary of the modelprint(model.summary())# Compile modelmodel.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])# Fit the modelmodel.fit(X_train, y_train, validation_data=(X_test, y_test), epochs=10, batch_size=200, verbose=2)# Final evaluation of the modelscores = model.evaluate(X_test, y_test, verbose=0)print("Baseline Error: %.2f%%" % (100-scores[1]*100))
Run the above code to double check your environment and get some sense about how it looks like while training a model. Design your own Neural Network to do Image Classification on Boat Dataset. Boat Dataset consists of 5 different types of boats:
Training Dataset (249.6 MB) Download
Class | Number of images |
---|---|
aircraft_carrier | 500 |
banana_boat | 500 |
oil_tanker | 500 |
passenger_ship | 500 |
yacht | 500 |
In total | 2500 |
Testing Dataset (97.1 MB, 1000 images) Download
Train your model on training dataset and test the trained model on testing dataset.
Hint
Try to understand the following API(s):
# All images in the new dataset are colorful images (consist of Red, Blue, and Green channels) with different sizes. You may would like to use the following API to resize all images into the same size before doing flatten (line 15 in the above code).image = cv2.imread(path) # load an imageresized_image = cv2.resize(image, (new_height, new_width)) # resize an image
To improve the performance of your model, you can try different values of the following parameters: